#! /bin/sh

#
# Helper functions for omi service control (Linux-specific)
#
# This script can be "sourced" (if sourcing with the "functions" qualifer),
# which may be used by the service control scripts. This allows for deeper
# control of the process at a low level.
#
# Otherwise (this is the normal case), invoke this with one of the following
# options:
#
#    start:    Start the OMI service via the service control manager
#    stop:     Stop the OMI service via the service control manager
#    restart:  Restart the OMI service via the service control manager
#    reload:   Reload agent configuration
#
# If the special marker file /etc/.omi_disable_service_control exists,
# OMI was not configured with service manager. This may be the case in a container
# environment, where service manager does not work reliably. Instead of relying on
# service manager to start/stop/restart OMI, we will instead invoke omiserver directly
# to start/stop/restart OMI.  See also the file installbuilder/datafiles/linux.data.

OMI_BIN=/opt/omi/bin/omiserver
PIDFILE=/var/opt/omi/run/omiserver.pid

verify_privileges()
{
    if [ `id -u` -ne 0 ]; then
        echo "Must have root privileges for this operation" >& 2
        exit 1
    fi
}

is_omiserverconf_exist()
{
    if [ -f /etc/opt/omi/conf/omiserver.conf ]; then
        return 0
    fi
    echo 'ERROR: omiserver.conf not found.'
    exit 3
}

is_omi_running()
{
    verify_privileges
    is_omiserverconf_exist

    # Returns 1 if 'omi' server is running, 0 otherwise
    [ -f $PIDFILE ] || return 0
    ps -p `cat $PIDFILE` | grep -q omiserver
    STATUS=$?

    # Process name not omiserver, then not running
    if [ $STATUS -ne 0 ]; then
        return 0
    else
        return 1
    fi
}

wait_until_omi_stops()
{
    # Required parameter: Number of seconds to wait for agent to stop
    if [ -z "$1" -o "$1" -le 0 ]; then
        echo "Function \"wait_until_omi_stops\" called with invalid parameter"
        exit 1
    fi

    COUNTER=$(( $1 * 2 )) # Since we sleep 0.5 seconds, compute number of seconds
    while [ $COUNTER -gt 0 ]; do
        [ ! -f $PIDFILE ] && return $?
        COUNTER=$(( $COUNTER - 1 ))
        sleep 0.5
    done

    # One final try for accurate return status (just return status from the call)
    is_omi_running
}

#
# Normal usage functions (used by everything except service control scripts)
#

start_omi()
{
    is_omi_running
    [ $? -ne 0 ] && return

    if [ -f /etc/.omi_disable_service_control ]; then
        /opt/omi/bin/omiserver -d
        return 0
    fi

    # try all possible ways to start omi
    echo 'Trying to start omi with systemctl'
    /bin/systemctl start omid >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo 'Trying to start omi with initctl'
        /sbin/initctl start omid >/dev/null 2>&1
    fi
    if [ $? -ne 0 ]; then
        echo 'Trying to start omi with /sbin/service'
        /sbin/service omid start >/dev/null 2>&1
    fi
    if [ $? -ne 0 ]; then
        echo 'Trying to start omi with /usr/bin/service'
        /usr/sbin/service omid start >/dev/null 2>&1
    fi
    if [ $? -ne 0 ]; then
        echo 'Trying to start omi with invoke-rc.d'
        /usr/sbin/invoke-rc.d omid start >/dev/null 2>&1
    fi
    if [ $? -ne 0 ]; then
        echo 'Trying to start omi with -d directly'
        /opt/omi/bin/omiserver -d >/dev/null 2>&1
    fi
    if [ $? -ne 0 ]; then
        echo "Unrecognized service controller to start omid service" 1>&2
        exit 2
    fi
    echo 'omi is started.'
}

stop_omi()
{
    is_omi_running 
    if [ $? -ne 0 ]; then
        if [ -f /etc/.omi_disable_service_control ]; then
            /opt/omi/bin/omiserver -s
            return 0
        fi

        # try all possible ways to stop omi
        echo 'Trying to stop omi with systemctl'
        /bin/systemctl stop omid >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo 'Trying to stop omi with initctl'
            /sbin/initctl stop omid >/dev/null 2>&1
        fi
        if [ $? -ne 0 ]; then
            echo 'Trying to stop omi with /sbin/service'
            /sbin/service omid stop >/dev/null 2>&1
        fi
        if [ $? -ne 0 ]; then
            echo 'Trying to stop omi with /usr/bin/service'
            /usr/sbin/service omid stop >/dev/null 2>&1
        fi
        if [ $? -ne 0 ]; then
            echo 'Trying to stop omi with invoke-rc.d'
            /usr/sbin/invoke-rc.d omid stop >/dev/null 2>&1
        fi
        if [ $? -ne 0 ]; then
            echo 'Trying to stop omi with -s directly'
            /opt/omi/bin/omiserver -s >/dev/null 2>&1
        fi
        if [ $? -ne 0 ]; then
            echo "Unrecognized service controller to start omid service" 1>&2
            exit 2  
        fi
    fi
    echo 'omi is stopped.'
}

restart_omi()
{
    is_omi_running
    if [ $? -eq 0 ]; then
        start_omi
        return
    fi

    stop_omi
    # wait omi to stop for 5 seconds
    wait_until_omi_stops 5
    start_omi
}

reload_omi()
{
    is_omi_running
    if [ $? -ne 0 ]; then
        # If systemd lives here, then we have a systemd unit file
        pidof systemd 1> /dev/null 2> /dev/null
        if [ $? -eq 0 ]; then
            /bin/systemctl reload omid
        else
            $OMI_BIN -r
        fi
    else
        start_omi
    fi
}

case "$1" in
    functions)
        ;;

    is-running)
        is_omi_running
        exit $?
        ;;

    start)
        start_omi
        ;;

    stop)
        stop_omi
        ;;

    restart)
        restart_omi
        ;;

    reload)
        # Old SCX packages deleted OMI linkages for SSL (very rude). This will
        # recreate them on service reload, which eases the problem. This can
        # be removed when upgrades from 2012R2 are out of scope and no longer
        # supported.
        #/opt/omi/bin/support/installssllinks

	# It appears that OMI has a bug where a 'reload' operation won't be
	# listening after a new agent install. For now, just have 'reload'
	# do an actual restart.
        restart_omi
        ;;

    *)
        echo "Unknown parameter : $1" 1>&2
        exit 1
        ;;
esac
